module.exports = function ($scope, $state, $resource, $modal, toaster) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};
	$scope.pageInfo = {};
	$scope.pageInfo.pageNo = 1;		//当前页码
	$scope.pageInfo.pageSize = 10;	//每页显示几条
	$scope.pageInfo.totalItems = 0;	//总条数
	//卡列表
	$scope.loadlist = function () {
		$resource('/api/as/mc/shopclass/pageList', {}, {}).save($scope.pageInfo, function (res) {
			if (res.errcode === 0) {
				$scope.beanList = res.data.results;
				$scope.pageInfo.totalItems = res.data.totalRecord;
				angular.forEach($scope.beanList, function(item){
					if(item.class_state == 1){
						item.selected = true;
					}else{
						item.selected = false;
					}
				});
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

	$scope.loadlist();

	//详情
	$scope.info = function (bean) {
		var modalInstance = $modal.open({
			template: require('../views/skwxClassInfo.html'),
			controller: 'skwxClassInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
	}

	//产品信息
	$scope.product = function (bean) {
      var modalInstance = $modal.open({
        template: require('../views/skwxClassProduct.html'),
        controller: 'skwxClassProduct',
        url: '/skwx/skwxClassProduct',
        size: 'lg',
        resolve: {
            'class_code': function () {
                return bean.class_code;
            }
        }
      });

      modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
      });
      modalInstance.result.then(function (showResult) {
      }, function (reason) {
      });
      // $state.go('app.skwxClassProduct', {'class_code' : bean.class_code});
		// var modalInstance = $modal.open({
		// 	template: require('../views/skwxClassProduct.html'),
		// 	controller: 'skwxClassProduct',
		// 	size: 'lg',
		// 	resolve: {
		// 		bean: function () {
		// 			return bean;
		// 		}
		// 	}
		// });
		// //关闭模态框刷新页面
		// modalInstance.result.then(function () {
		// 	$scope.loadlist();
		// });
	}

	//删除卡
	$scope.delete = function (bean) {
		if (confirm('确认删除此条记录吗？') == true) {
			$resource('/api/as/mc/shopclass/delete', {}, {}).save(bean, function (res) {
				if (res.errcode === 0) {
					$scope.loadlist();
				} else {
					toaster.error({ title: '', body: res.errmsg });
				}
			});
		}
	}

	//设置首页显示不显示
	$scope.change = function(info) {
		
		var para = {
			'class_state': info.selected ? '1' : '0',
			'id': info.id
		}
		$resource('/api/as/mc/shopclass/update', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '设置成功。'});
			}else{
				toaster.error({title: '', body: '设置失败请重试。'});
			}
		});
	}
};